package cn.cosmosx.controller.sys;

import cn.cosmosx.base.encrypt.aes.AESUtil;
import cn.cosmosx.base.encrypt.loader.SM2PFXCertGenerateUtil;
import cn.cosmosx.base.encrypt.sm.KeyType;
import cn.cosmosx.base.encrypt.sm.SM2Util;
import cn.cosmosx.base.encrypt.sm.SM4Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Api(tags = "证书管理")
@RestController
@RequestMapping("/sys/cert")
public class CertificateController {

    @ApiOperation(value = "生成SM2证书PFX（pkcs12）")
    @GetMapping("/generate/sm2cert")
    public void generateSM2cert(@RequestParam(value = "alias", defaultValue = "myCert") String alias,
                                @RequestParam(value = "password", defaultValue = "123456") String password,
                                HttpServletResponse response) throws Exception {
        SM2PFXCertGenerateUtil.savePfx(alias, password, "SM2Cert.pfx", response);
    }

    @ApiOperation(value = "生成SM2公私钥对")
    @PostMapping("/generate/sm2keypair")
    public Map<KeyType, String> generateSM2KeyPair() {
        return SM2Util.generateKeyPair();
    }

    @ApiOperation(value = "生成SM4秘钥")
    @PostMapping("/generate/sm4key")
    public String generateSM4key() {
        return SM4Util.generateKey();
    }

    @ApiOperation(value = "生成AES秘钥")
    @PostMapping("/generate/aesKey")
    public String generateAesKey() {
        return AESUtil.generateSecretHex();
    }

}